package com.cookor.poi.utils;

import com.alibaba.fastjson.JSON;
import com.cookor.poi.common.cache.MysqlCacheManager;
import com.cookor.poi.common.utils.SpringContextHolder;
import org.apache.commons.lang3.StringUtils;

public class DbCacheUtils {
	private static MysqlCacheManager cacheManager = ((MysqlCacheManager) SpringContextHolder.getBean("mysqlCacheManager"));
	private static final String SYS_CACHE = "sysCache";

	/**
	 * 获取缓存
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public static Object get(String cacheName, String key) {
		String value = cacheManager.get(cacheName, key);
		if(StringUtils.isNotBlank(value)) {
			return JSON.parse(value);
		}
		return null;
	}
	public static Object get(String key) {
		return get(SYS_CACHE,key);
	}
	public static void put(String key, Object value) {
		 put(SYS_CACHE,key,value);
	}

	/**
	 * 写入缓存
	 * @param cacheName
	 * @param key
	 * @param value
	 */
	public static void put(String cacheName, String key, Object value) {
		cacheManager.put(cacheName, key, JSON.toJSONString(value));
	}

	public static void put(String cacheName, String key, Object value, int nExpireSecond, int nIdleSecond) {
		cacheManager.put(cacheName, key, JSON.toJSONString(value),nExpireSecond,nIdleSecond);
	}

	/**
	 * 从缓存中移除
	 * @param cacheName
	 * @param key
	 */
	public static void remove(String cacheName, String key) {
		cacheManager.delete(cacheName,key);
	}

	/**
	 * 从缓存中移除
	 * @param cacheName
	 */
	public static void removeCache(String cacheName) {
		cacheManager.deleteCache(cacheName);
	}
}
